package cn.fly.springbucks.service.impl;

import cn.fly.springbucks.data.mybatis.mapper.CoffeeMapper;
import cn.fly.springbucks.data.mybatis.model.Coffee;
import cn.fly.springbucks.data.redis.CoffeeCache;
import cn.fly.springbucks.data.repository.CoffeeCacheRepository;
import cn.fly.springbucks.service.CoffeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.joda.money.Money;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
@Slf4j
public class CoffeeServiceImpl implements CoffeeService {

    @Autowired
    CoffeeMapper coffeeMapper;

    @Value("3")
    int pageSize;

    @Autowired
    private CoffeeCacheRepository coffeeCacheRepository;

    @Override
    @Transactional
    public List<Coffee> getAllCoffeeMenu(int page, int type) {

        List<Coffee> coffees = new ArrayList<>();

        if (type == 0) {
            coffees = coffeeMapper.findAllWithRowBounds(new RowBounds(page == 0 ? 1 : page, page == 0 ? 0 : pageSize));
        } else {
            coffees = coffeeMapper.findAllWithParam(page == 0 ? 1 : page, page == 0 ? 0 : pageSize);
        }
        return coffees;
    }

    @Override
    @Transactional
    public Coffee getCoffeeByName(String name) {
        CoffeeCache cached = coffeeCacheRepository.getCoffeeCachesByName(name);
        if (cached != null) {
            System.out.println("=========================redis缓存查询===========================");
            CoffeeCache coffeeCache = cached;
            Coffee coffee = Coffee.builder()
                    .name(coffeeCache.getName())
                    .price(coffeeCache.getPrice())
                    .build();
            return coffee;
        } else {
            System.out.println("=========================数据库查询===========================");
            Coffee coffee = coffeeMapper.getCoffeeByName(name);
            CoffeeCache coffeeCache = CoffeeCache.builder()
                    .id(coffee.getId())
                    .name(coffee.getName())
                    .price(coffee.getPrice())
                    .build();
            coffeeCacheRepository.save(coffeeCache);
            return coffee;
        }
    }

    @Override
    @Transactional
    public int updatePriceByIDTran (List<Coffee> list) throws NullPointerException{
        String name;
        Money price;
        int num = 0;
        for (int i = 0;i<list.size();i++) {
            if (i==3)
                throw new NullPointerException();
            name = list.get(i).getName();
            price = list.get(i).getPrice();
            coffeeMapper.updatePriceByID(name, price);
        }
        return num;
    }

    @Override
    public int updatePriceByID(List<Coffee> list) throws NullPointerException{
        String name;
        Money price;
        int num = 0;
        for (int i = 0;i<list.size();i++) {
            if (i==3)
                throw new NullPointerException();
            name = list.get(i).getName();
            price = list.get(i).getPrice();
            coffeeMapper.updatePriceByID(name, price);
        }
        return num;
    }
}
